Skip to content

Image Editor: Remove floating-point noise from the edit read-back - #87376

Merged
ramonjd merged 3 commits into
trunkfrom
update/image-editor-snap-read-back
Nov 25, 2026
Merged

ramonjd merged 3 commits into
trunkfrom
update/image-editor-snap-read-back

Conversation

@ramonjd

@ramonjd ramonjd commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

What?

Follow-up to #86665.

getImageEdit and getOutputSize return a value that is within a millionth of a whole number as that number.

Screenshot 2026-11-25 at 1 47 25 pm

A crop fraction within a millionth of 0 or 1 reads as 0 or 1, and a dimension within a millionth of a pixel of a whole number reads as that pixel count.

Why?

Working the edit back out of the editor state goes through the pan and zoom, which leaves floating-point noise.

On a 1600 x 900 image, a crop of nine tenths read back as 1439.9999999999998 by 809.9999999999999 pixels, a crop on the left edge started at y: 2.2992026060819626e-9, and a quarter turn gave a width of 900.0000000000001.

Anything that shows or compares these values has to clean them up first.

Example: an agent driving applyImageEdit over MCP spent words on every one of them.

Rotation is not touched: it is the number the person set, and no run showed noise in it.

Testing Instructions

  1. Run npm run build.
  2. From the repository root, run:
node -e 'import("./packages/image-editor/build-module/index.mjs").then(({ createImageEditorState, applyImageEdit, getImageEdit, getOutputSize }) => {
  const state = createImageEditorState({ image: { src: "a.jpg", naturalWidth: 1600, naturalHeight: 900 } });
  for (const edit of [{ crop: { x: 0.05, y: 0.05, width: 0.9, height: 0.9 } }, { crop: { x: 0, y: 0, width: 0.5, height: 1 } }, { rotation: 90 }]) {
    const { state: next } = applyImageEdit(state, edit);
    console.log(JSON.stringify(getImageEdit(next)), JSON.stringify(getOutputSize(next)));
  }
});'
  1. Expect whole numbers for the sizes and 0 for the edge of the second crop:
{"crop":{"x":0.05000000000000002,"y":0.05000000000000002,"width":0.8999999999999999,"height":0.8999999999999999}} {"width":1440,"height":810}
{"crop":{"x":0,"y":0,"width":0.5,"height":1}} {"width":800,"height":900}
{"rotation":90} {"width":900,"height":1600}

On trunk:

{"crop":{"x":0.05000000000000002,"y":0.05000000000000002,"width":0.8999999999999999,"height":0.8999999999999999}} {"width":1439.9999999999998,"height":809.9999999999999}
{"crop":{"x":0,"y":2.2992026060819626e-9,"width":0.5,"height":1}} {"width":800,"height":900}
{"rotation":90} {"width":900.0000000000001,"height":1600}

The same command on trunk prints 1439.9999999999998, 809.9999999999999, "y":2.2992026060819626e-9 and 900.0000000000001.

The crop fractions of the first edit stay as they are: 0.05000000000000002 is not near a whole number, and the size is what the snap is for.

…tputSize

Working the edit back out of the state could put a crop edge at 7e-8 instead of 0, and give a size of 1439.9999999999998 pixels instead of 1440. A value within a millionth of a whole number is now returned as that number.
@ramonjd ramonjd added [Type] Bug An existing feature does not function as intended [Package] Image Editor Related to packages/image-editor labels Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

📦 Bundle size

Size Change: +94 B (0%)

Total Size: 8.71 MB

📦 View Changed
Filename Size Change
build/scripts/editor/index.min.js 574 kB +94 B (+0.02%)

3c645ae Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 3d5e85e trunk % Change
timeToFirstByte 58.35 ms +8.91% -4.63% 56.8 ms +1.14% -3.43% 2.73%
largestContentfulPaint 94 ms +10.64% -6.38% 88 ms +2.27% -4.55% 6.82%
lcpMinusTtfb 35.3 ms +13.17% -15.58% 30.65 ms +9.46% -10.77% 15.17%
wpBeforeTemplate 30.57 ms +14.3% -3.83% 29.53 ms +2.13% -2.68% 3.52%
wpTemplate 22.99 ms +9.57% -3.39% 23.1 ms +2.16% -6.02% -0.48%
wpTotal 54.25 ms +9.33% -4.26% 52.88 ms +1.46% -3.5% 2.59%
wpMemoryUsage 7.75 MB +0% -0% 7.72 MB +0% -0% 0.45%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 3d5e85e trunk % Change
timeToFirstByte 51.6 ms +3.88% -3.78% 46.6 ms +4.94% -1.72% 10.73%
largestContentfulPaint 92 ms +0% -4.35% 80 ms +7.5% -0% 15%
lcpMinusTtfb 38.35 ms +1.96% -1.43% 34.15 ms +5.71% -1.76% 12.3%
wpBeforeTemplate 28.68 ms +11.12% -2.48% 26.61 ms +1.13% -1.01% 7.78%
wpTemplate 18.55 ms +6.85% -3.5% 16.85 ms +3.74% -3.98% 10.09%
wpTotal 48.16 ms +3.99% -3.47% 43.51 ms +5.22% -1.77% 10.69%
wpMemoryUsage 6.44 MB +0% -0% 6.41 MB +0% -0% 0.56%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 3d5e85e trunk % Change
mediaProcessingJpeg 398.73 ms +1.11% -0.27% 402.32 ms +1.48% -0.42% -0.89%
mediaProcessingAvif 6158.07 ms +0.16% -0.35% 6189.56 ms +0.06% -0.4% -0.51%
mediaProcessingJpegToAvif 4242.84 ms +0.56% -0.03% 4271.83 ms +0.22% -0.41% -0.68%

media-upload

Metric 3d5e85e trunk % Change
jpegUploadProcessing 1991.01 ms +1.92% -23.79% 1973.94 ms +1.26% -22.69% 0.86%
pngUploadProcessing 274 ms +0.32% -1.26% 288.48 ms +4.18% -5.46% -5.02%
largeJpegUploadProcessing 1760.23 ms +12.14% -12.09% 1758.08 ms +13.37% -13.98% 0.12%
multipleImageUploadProcessing 2082.74 ms +1.64% -0.47% 2077.27 ms +2.69% -0.28% 0.26%

post-editor

Metric 3d5e85e trunk % Change
serverResponse 422.19 ms +0.89% -5.75% 387.42 ms +7.91% -1.04% 8.97%
firstPaint 409.15 ms +13.3% -2.13% 391.71 ms +8.92% -2.32% 4.45%
domContentLoaded 823.43 ms +4.59% -1.35% 811.38 ms +1.26% -0.74% 1.49%
loaded 824.36 ms +4.6% -1.32% 812.43 ms +1.26% -0.75% 1.47%
firstContentfulPaint 467.79 ms +7.57% -2.85% 453.13 ms +1.33% -1.13% 3.24%
firstBlock 2187.04 ms +2.44% -2.85% 2130.29 ms +1.1% -0.53% 2.66%
type 17.51 ms +9.88% -8.17% 17.35 ms +5.94% -13.14% 0.92%
typeWithoutInspector 17.74 ms +2.25% -7.16% 17.44 ms +10.38% -8.49% 1.72%
typeWithTopToolbar 23.44 ms +8.4% -4.1% 23.74 ms +4.47% -3.03% -1.26%
typeContainer 7.81 ms +5.51% -8.19% 7.93 ms +6.43% -8.7% -1.51%
focus 121.38 ms +2.39% -4.95% 116.92 ms +4.11% -5.91% 3.81%
firstFocus 265.47 ms +0% -0% 276.97 ms +0% -0% -4.15%
selectAll 361.83 ms +3.74% -2.07% 333.26 ms +8% -1.71% 8.57%
undo 29.73 ms +6.16% -2.99% 31 ms +6.94% -3.29% -4.1%
listViewOpen 38.38 ms +3.02% -7.11% 38.72 ms +3.18% -6.22% -0.88%
inserterOpen 43.77 ms +4.09% -2.38% 43.45 ms +6.81% -6.9% 0.74%
inserterHover 11.77 ms +11.3% -9.09% 11.61 ms +3.88% -8.7% 1.38%
inserterSearch 9.22 ms +5.53% -9.22% 9.02 ms +9.53% -9.53% 2.22%
loadPatterns 711.95 ms +2.14% -8.52% 697.07 ms +7.37% -2.75% 2.13%
wpTotal 412.46 ms +0.7% -5.85% 377.57 ms +8.22% -1% 9.24%
wpMemoryUsage 14.80 MB +0% -0% 14.76 MB +0% -0% 0.25%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric 3d5e85e trunk % Change
serverResponse 467.1 ms +0.73% -7.71% 466.29 ms +0.52% -8.88% 0.17%
firstPaint 417.81 ms +13.98% -8.75% 455.15 ms +2.47% -12.24% -8.2%
domContentLoaded 841.52 ms +7.53% -2.03% 833.09 ms +1.2% -0.67% 1.01%
loaded 842.52 ms +7.55% -2.01% 834.04 ms +1.19% -0.67% 1.02%
firstContentfulPaint 472.51 ms +6.66% -2.72% 462.43 ms +1.78% -1.1% 2.18%
firstBlock 2611.3 ms +2.75% -1.32% 2580.74 ms +0.81% -1.05% 1.18%
type 17.8 ms +4.38% -16.46% 16.7 ms +2.4% -1.32% 6.59%
navigate 110.18 ms +7.53% -1.19% 106.62 ms +8.95% -1.43% 3.34%
loadPatterns 1444.18 ms +3.74% -8.48% 1239.84 ms +8.44% -4.96% 16.48%
loadPages 966.63 ms +3.21% -2.61% 945.37 ms +4.98% -1.33% 2.25%
wpTotal 457.66 ms +0.76% -7.85% 456.94 ms +0.5% -9.06% 0.16%
wpMemoryUsage 12.84 MB +0% -0% 12.80 MB +0% -0% 0.28%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

3c645ae Run

@ramonjd ramonjd self-assigned this Nov 25, 2026
@ramonjd
ramonjd requested a review from andrewserong November 25, 2026 02:52

@andrewserong andrewserong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice follow-up!

* pixels. A millionth, of the image or of a pixel, is far below anything
* a person can set, so a value that close to a whole number is that number.
*/
const WHOLE_NUMBER_TOLERANCE = 1e-6;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're using 1e-6 in a few places. Not for now, but if we could potentially consolidate them if we think it's a universal tolerance we want to use? E.g. there's also:

const STATE_EPSILON = 1e-6;

Not a big deal to have them separate if you like the clarity of differently named constants, though!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, it's in a few places. I guess it's common enough to warrant centralization. Thanks for spotting. I'll do a follow up if I can come up with a decent enough const name 😄

* pixels. A millionth, of the image or of a pixel, is far below anything
* a person can set, so a value that close to a whole number is that number.
*/
const WHOLE_NUMBER_TOLERANCE = 1e-6;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, it's in a few places. I guess it's common enough to warrant centralization. Thanks for spotting. I'll do a follow up if I can come up with a decent enough const name 😄

@ramonjd
ramonjd enabled auto-merge (squash) November 25, 2026 04:41
@ramonjd
ramonjd merged commit d7e2c5c into trunk Nov 25, 2026
85 checks passed
@ramonjd
ramonjd deleted the update/image-editor-snap-read-back branch November 25, 2026 04:49
@github-actions github-actions Bot added this to the Gutenberg 24.6 milestone Nov 25, 2026
@ramonjd

ramonjd commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

I'll do a follow up if I can come up with a decent enough const name

So the shared value is more of a coincidence I feel. There are six! But they apply to different things like degrees, pixels, zoom etc. A single constant could mean that they all share the same boundaries, and would make it harder to customize them later. I'll leave for now but keep it mind. Thanks again!

@andrewserong

Copy link
Copy Markdown
Contributor

A single constant could mean that they all share the same boundaries, and would make it harder to customize them later. I'll leave for now but keep it mind. Thanks again!

All good! Thanks for looking into it, and yeah I can imagine us eventually wanting different levels of granularity, good to keep it as is for now 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Image Editor Related to packages/image-editor [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants